package bag.servlet;

import bag.domain.User;
import bag.services.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by 冷 on 2017/9/29.
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("user");
        String pword = request.getParameter("pass");
        UserService userService = new UserService();
        User user = null;
        try {
            user = userService.findByUsernameAndPassword(uname,pword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(user != null){//如果user存在 执行
            //重定向
            response.sendRedirect("/untitled3/hello.jsp");
        }else {
            response.sendRedirect("/untitled3/login.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
